package com.movie.controller;

import com.movie.entity.Showtime;
import com.movie.service.ShowtimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api") // 基础路径为 /api
@CrossOrigin(origins = "http://localhost:5173", allowCredentials = "true")
public class ShowtimeController {

    @Autowired
    private ShowtimeService showtimeService;

    /**
     * 根据电影 ID 获取该电影的所有可用场次
     * @param movieId 电影 ID
     * @return 场次列表
     */
    @GetMapping("/movies/{movieId}/showtimes")
    public ResponseEntity<List<Showtime>> getShowtimesByMovie(@PathVariable Long movieId) {
        List<Showtime> showtimes = showtimeService.getShowtimesByMovieId(movieId);
        // 如果没有场次，返回空列表是正常的，不需要返回 404
        return ResponseEntity.ok(showtimes);
    }

    /**
     * 根据场次 ID 获取场次详情
     * @param id 场次 ID
     * @return 场次详情
     */
    @GetMapping("/showtimes/{id}")
    public ResponseEntity<Showtime> getShowtimeById(@PathVariable Long id) {
        try {
            Showtime showtime = showtimeService.getShowtimeById(id);
            if (showtime != null) {
                return ResponseEntity.ok(showtime);
            } else {
                return ResponseEntity.notFound().build();
            }
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    // 未来可以添加管理员相关的场次管理接口到 AdminShowtimeController
}